package team.fivez.defaultmanage.dao.Impl;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import team.fivez.defaultmanage.dao.DefaultReasonDao;
import team.fivez.defaultmanage.domain.DefaultReason;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class DefaultReasonDaoImpl implements DefaultReasonDao {

    @Resource
    private JdbcTemplate jdbcTemplate;

    //获取所有的原因
    @Override
    public List<DefaultReason> getList() {

        String sql="select * from default_reason";

        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(DefaultReason.class));

    }

    //获取已经启用的原因列表
    @Override
    public List<DefaultReason> getUsedReasonList() {

        String sql="select * from default_reason where used_status=1";

        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(DefaultReason.class));
    }

    //更改某一条原因的启用状态
    @Override
    public void updateUsedStatusByNumber(int number,int status) {
        String sql="update default_reason set used_status=? where default_reason_number=?";

        jdbcTemplate.update(sql,status,number);

    }

    //通过数字序号查找原因
    @Override
    public DefaultReason getDefaultReasonByNumber(int number) {
        String sql="select * from default_reason where default_reason_number =?";
        return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(DefaultReason.class),number);
    }
}
